import Vue from 'vue'
import axios from 'axios'
let cancelToken = axios.CancelToken
// var CancelToken = axios.CancelToken;
// var source = CancelToken.source();
// axios.get('/user/12345', {//get请求在第二个参数
//     cancelToken: source.token
// }).catch(function(thrown) {
// });
// axios.post('/user/12345', {//post请求在第三个参数
//     name: 'new name'
// }, {
//     cancelToken: source.token
// });
// source.cancel('不想请求了');

axios.interceptors.request.use(suc => {return suc}, err=>{return err})
axios.interceptors.response.use(suc => {return suc}, err=>{
    console.log(err.response.status)
    ;return err})
let header = {
    name: '1'
}
let objCallback = function (method ,url, data) {
    let obj = {
        url: url,
        data: data,
        timeout: 5000,
        baseURL: 'http://localhost:3000',
        params: {
            ...data
        },
        method: method,
        headers: {
            ...header
        },
        cancelToken: new cancelToken(function (res) {
            // res('121') 执行就会 取消发送请求
        })
    }
    if (method === 'post') {
        delete obj.params
    }
    if (method === 'get') {
        delete obj.data
    }
    return axios(obj)
}
let httpAxios = function (method,url,data) {
    let http = objCallback(method,url,data)
    return http
}

Vue.prototype.$http = httpAxios
export default () => {return httpAxios}